import Vue from 'vue';
import VueRouter from 'vue-router';

const Home = () => import('@/views/home/Home');
const Category = () => import('@/views/category/Category');
const CategoryList = () => import('@/views/category/children/CategoryList');
const Order = () => import('@/views/order/Order');
const OrderDetail = () => import('@/views/order/children/OrderDetail');
const OrderComment = () => import('@/views/order/children/children/Comment');
const User = () => import('@/views/user/User');
const CollectList = () => import('@/views/user/children/CollectList');
const UserInfo = () => import('@/views/user/children/UserInfo');
const AddressList = () => import('@/views/user/address/AddressList');
const NewAddress = () => import('@/views/user/address/NewAddress');
const EditAddress = () => import('@/views/user/address/EditAddress');
const Password = () => import('@/views/user/children/children/Password');
const Phone = () => import('@/views/user/children/children/Phone');
const Login = () => import('@/views/login/Login');
const SelectAddress = () => import('@/views/city/SelectAddress');
const SearchCity = () => import('@/views/city/SearchCity');
const Shop = () => import('@/views/shop/Shop.vue');
const Menu = () => import('@/views/shop/menu/Menu');
const ShopComment = () => import('@/views/shop/comment/Comment');
const Detail = () => import('@/views/shop/detail/Detail');
const ConfirmOrder = () => import('@/views/confirmOrder/ConfirmOrder');
const Remarks = () => import('@/views/confirmOrder/children/Remarks');
const Payment = () => import('@/views/payment/Payment');
const Search = () => import('@/views/search/Search');

Vue.use(VueRouter);

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/home', component: Home, meta: { keepAlive: true } },
  {
    path: '/category',
    name: 'Category',
    component: Category,
    children: [
      {
        path: 'list',
        name: 'CategoryList',
        component: CategoryList
      }
    ]
  },
  {
    path: '/selectAddress',
    name: 'SelectAddress',
    component: SelectAddress
  },
  { path: '/searchCity', name: 'SearchCity', component: SearchCity },
  { path: '/login', name: 'Login', component: Login },
  {
    path: '/shop',
    name: 'Shop',
    component: Shop,
    children: [
      { path: 'menu', name: 'Menu', component: Menu },
      { path: 'comment', name: 'ShopComment', component: ShopComment },
      { path: 'detail', name: 'Detail', component: Detail }
    ]
  },
  {
    path: '/confirmOrder',
    name: 'ConfirmOrder',
    component: ConfirmOrder,
    children: [{ path: 'remarks', name: 'Remarks', component: Remarks }]
  },
  {
    path: '/order',
    name: 'Order',
    component: Order,
    children: [
      {
        path: 'detail',
        name: 'OrderDetail',
        component: OrderDetail,
        children: [
          { path: 'comment', name: 'OrderComment', component: OrderComment }
        ]
      }
    ]
  },
  {
    path: '/user',
    name: 'User',
    component: User,
    children: [
      { path: 'collect', name: 'CollectList', component: CollectList },
      {
        path: 'address',
        name: 'AddressList',
        component: AddressList,
        children: [
          {
            path: 'newAddress',
            name: 'NewAddress',
            component: NewAddress,
            meta: { keepAlive: true }
          },
          {
            path: 'editAddress',
            name: 'EditAddress',
            component: EditAddress,
            meta: { keepAlive: true }
          }
        ]
      },
      {
        path: 'info',
        name: 'UserInfo',
        component: UserInfo,
        children: [
          { path: 'password', name: 'Password', component: Password },
          { path: 'phone', name: 'Phone', component: Phone }
        ]
      }
    ]
  },
  { path: '/payment', name: 'Payment', component: Payment },
  { path: '/search', name: 'Search', component: Search }
];

// 修改路由重复跳转问题
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
// 修改原型对象中的方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};

const router = new VueRouter({
  routes
});

export default router;
